/*
@title: Score_it
@description: "Score_it" is a progressive football-themed game where the objective is to shoot the ball into the goal across 10 challenging levels. Each level increases in difficulty as players navigate different obstacles to score.
@author: asmahdi08
@tags: ['sports']
@addedOn: 2024-08-22
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const player = "p"
const goal_u = "u"
const goal_l = "l"
const goal_r = "r"
const green = "g"
const black = "b"
const obstacle = "o"
const level1 = map`
bbbbbbbbbbbbb
ggggggggggggg
ggggglurggggg
gggggggoggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level2 = map`
bbbbbbbbbbbbb
ggggggggggggg
ggggglurggggg
ggggggooggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level3 = map`
bbbbbbbbbbbbb
ggggggggggggg
ggggglurggggg
ggggggogggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level4 = map`
bbbbbbbbbbbbb
ggggggggggggg
gggluuuuurggg
gggogoooogggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level5 = map`
bbbbbbbbbbbbb
ggggggggggggg
ggggglurggggg
ggggggooggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level6 = map`
bbbbbbbbbbbbb
ggggggggggggg
gggluuuuurggg
gggogoooogggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level7 = map`
bbbbbbbbbbbbb
ggggggggggggg
gggluuuuurggg
gggoooooogggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level8 = map`
bbbbbbbbbbbbb
ggggggggggggg
ggggggggglurg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level9 = map`
bbbbbbbbbbbbb
ggggggggggggg
gluuuuuuuuurg
gogooooooogog
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const level10 = map`
bbbbbbbbbbbbb
ggggggggggggg
ggggggggggggl
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggggggggg
ggggggpgggggg
uuuuuuuuuuuuu`
const backgroundTune = tune`
153.0612244897959: B5-153.0612244897959 + C4~153.0612244897959 + F4^153.0612244897959,
153.0612244897959: G5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: F5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: G5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: G5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: E5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: D5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: D5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: C5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: E5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: D5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: C5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: E5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: F5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: G5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: F5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: F5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: E5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: B4-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: B4-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: C5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: A4-153.0612244897959 + G4-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: C5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: A4-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: E5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: D5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: B4-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: B4-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: C5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: E5-153.0612244897959 + E4/153.0612244897959,
153.0612244897959: F5-153.0612244897959 + C4~153.0612244897959,
153.0612244897959: B5-153.0612244897959 + C4~153.0612244897959 + F4^153.0612244897959`
const goalTune = tune`
130.43478260869566,
130.43478260869566: A4/130.43478260869566 + G4-130.43478260869566,
130.43478260869566: B4-130.43478260869566 + C5/130.43478260869566,
130.43478260869566: D5-130.43478260869566 + E5/130.43478260869566,
130.43478260869566: F5-130.43478260869566 + G5/130.43478260869566,
130.43478260869566,
130.43478260869566: G5-130.43478260869566 + A5/130.43478260869566,
130.43478260869566: G5-130.43478260869566 + A5/130.43478260869566,
3130.434782608696`
const bgTune = tune`
410.958904109589: B4~410.958904109589 + A4~410.958904109589 + G4~410.958904109589 + C4^410.958904109589 + E5^410.958904109589,
410.958904109589: F4~410.958904109589 + E4~410.958904109589,
410.958904109589: B4~410.958904109589 + A4~410.958904109589 + G5/410.958904109589,
410.958904109589: D5~410.958904109589 + E5~410.958904109589 + F5~410.958904109589 + C4^410.958904109589,
410.958904109589: G4~410.958904109589 + F4~410.958904109589 + C4^410.958904109589,
410.958904109589: C5~410.958904109589 + B4~410.958904109589 + E5^410.958904109589,
410.958904109589: C5~410.958904109589 + C4^410.958904109589 + E5^410.958904109589,
410.958904109589: F4~410.958904109589 + C4^410.958904109589,
410.958904109589: A4~410.958904109589 + G4~410.958904109589 + F4~410.958904109589 + G5/410.958904109589,
410.958904109589: F4~410.958904109589 + C4^410.958904109589,
410.958904109589: B4~410.958904109589 + E4~410.958904109589 + D4~410.958904109589 + C4^410.958904109589,
410.958904109589: F4~410.958904109589 + E5^410.958904109589,
410.958904109589: A4~410.958904109589 + G4~410.958904109589 + C4^410.958904109589 + E5^410.958904109589,
410.958904109589: A4~410.958904109589 + B4~410.958904109589 + E4~410.958904109589 + D4~410.958904109589 + C4^410.958904109589,
410.958904109589: A4~410.958904109589 + G5/410.958904109589,
410.958904109589: A4~410.958904109589 + E5~410.958904109589 + D5~410.958904109589 + C4^410.958904109589,
410.958904109589: C4^410.958904109589,
410.958904109589: A4~410.958904109589 + B4~410.958904109589 + F4~410.958904109589 + E5^410.958904109589,
410.958904109589: F4~410.958904109589 + C4^410.958904109589 + E5^410.958904109589,
410.958904109589: C4^410.958904109589,
410.958904109589: A4~410.958904109589 + G4~410.958904109589 + B4~410.958904109589 + G5/410.958904109589,
410.958904109589: A4~410.958904109589 + C4^410.958904109589,
410.958904109589: G4~410.958904109589 + C4^410.958904109589,
410.958904109589: B4~410.958904109589 + A4~410.958904109589 + E5^410.958904109589,
410.958904109589: D5~410.958904109589 + F4~410.958904109589 + G4~410.958904109589 + C4^410.958904109589 + E5^410.958904109589,
410.958904109589: E4~410.958904109589 + C4^410.958904109589,
410.958904109589: D4~410.958904109589 + G5/410.958904109589,
410.958904109589: G4~410.958904109589 + B4~410.958904109589 + A4~410.958904109589 + C4^410.958904109589,
410.958904109589: D5~410.958904109589 + C4^410.958904109589 + E5^410.958904109589,
410.958904109589: A4~410.958904109589 + F4~410.958904109589 + E4~410.958904109589 + E5^410.958904109589,
410.958904109589: F5~410.958904109589 + E5~410.958904109589 + D5~410.958904109589 + C4^410.958904109589,
410.958904109589: A4~410.958904109589 + B4~410.958904109589 + G4~410.958904109589 + C4^410.958904109589 + G5/410.958904109589`
const winningTune = tune`
94.9367088607595: C4/94.9367088607595 + G4/94.9367088607595 + D5-94.9367088607595,
94.9367088607595: D4/94.9367088607595 + A4/94.9367088607595 + E5-94.9367088607595,
94.9367088607595: E4/94.9367088607595 + B4/94.9367088607595 + F5-94.9367088607595 + C4/94.9367088607595,
94.9367088607595: F4/94.9367088607595 + C5/94.9367088607595 + G5-94.9367088607595,
94.9367088607595: G4/94.9367088607595 + D5/94.9367088607595 + A5-94.9367088607595 + C4/94.9367088607595,
94.9367088607595: A4/94.9367088607595 + E5/94.9367088607595 + B5/94.9367088607595,
94.9367088607595: B4/94.9367088607595 + F5/94.9367088607595 + B5-94.9367088607595 + C4/94.9367088607595,
94.9367088607595: G5/94.9367088607595 + C5/94.9367088607595 + B5/94.9367088607595,
94.9367088607595: F5/94.9367088607595 + B4/94.9367088607595 + B5-94.9367088607595 + C4/94.9367088607595,
94.9367088607595: E5/94.9367088607595 + A4/94.9367088607595 + B5/94.9367088607595,
94.9367088607595: D5/94.9367088607595 + G4/94.9367088607595 + A5-94.9367088607595 + C4/94.9367088607595,
94.9367088607595: C5/94.9367088607595 + F4/94.9367088607595 + G5-94.9367088607595,
94.9367088607595: B4/94.9367088607595 + E4/94.9367088607595 + F5-94.9367088607595 + C4/94.9367088607595,
94.9367088607595: A4/94.9367088607595 + D4/94.9367088607595 + E5-94.9367088607595,
94.9367088607595: G4/94.9367088607595 + C4/94.9367088607595 + D5-94.9367088607595,
94.9367088607595: C5-94.9367088607595,
1518.987341772152`
const loseTune = tune`
153.0612244897959: E5^153.0612244897959,
153.0612244897959: D5~153.0612244897959,
153.0612244897959: C5^153.0612244897959,
153.0612244897959: B4~153.0612244897959,
153.0612244897959: G4^153.0612244897959,
153.0612244897959: F4~153.0612244897959,
153.0612244897959: E4^153.0612244897959,
153.0612244897959: D4~153.0612244897959,
153.0612244897959: C4^153.0612244897959,
3520.408163265306`
const introColor = color`5`
let currentmap = 1
let vainterval

let playback = playTune(backgroundTune, Infinity)

setLegend(
  [player, bitmap`
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDD0000DDDDDD
DDDD00022000DDDD
DDDD00222000DDDD
DDD0020202200DDD
DDD0222000220DDD
DDD0222002020DDD
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DDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD`],
  [obstacle, bitmap`
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDD0DDDDDDDD
DDDDDD090DDDDDDD
DDDDD09990DDDDDD
DDDD0999990DDDDD
DDD099999990DDDD
DD09999999990DDD
D0999999999990DD
011111116666660D
000000000000000D`],
  [black, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`]
)

setSolids([player, goal_u, goal_l, goal_r, obstacle, black])

let level = 0
const levels = [
  map`
ggggggggggg
gggglurgggg
ggggggggggg
ggggggggggg
ggggggggggg
ggggggggggg
ggggggggggg
ggggggggggg
uuuuuuuuuuu`,
  level1,
  level2,
  level3,
  level4,
  level5,
  level6,
  level7,
  level8,
  level9,
  level10,
]

setMap(levels[1])

setPushables({
  [player]: []
})

setBackground("g")

let movcon = 1
let ycon = 0
let turner = 0
let movinterval = 200
let FirstInput = 0
let finished = 0
let automover;

let levtext
let LostTxt

function checkPlayerCollision(obj) {
  let obstacles = getAll(obj);
  for (let obstacleSprite of obstacles) {
    if (getFirst(player).x === obstacleSprite.x && getFirst(player).y === (obstacleSprite.y+1)) {
      return 1;
    }
  }
  return 0;
}
function changeLevel(levelnum){
  
  setMap(levels[levelnum])
  ycon = 0
  movcon =1
  turner =0
  if(finished == 1||finished == 2){
    currentmap = 1
    movinterval = 200
    playback = playTune(backgroundTune, Infinity)
  }
  else {currentmap += 1}
  if(levelnum >= 5) {
    movinterval = 90
  }
  vainterval = setInterval(mainfunc,movinterval)
  clearText(levtext)
  levtext = addText(("Lvl: " + currentmap), options = { x:0, y:0, color:color`9` })
}

function handleLost(){
  LostTxt = addText("You Lost :(\n\nPress any key\nto go to the\nfirst level", options = { x:4, y:5, color:introColor })
  finished = 2
}


function mainfunc(){
      if (getFirst(player).x == 0) {
      turner = 1;
    } else if (getFirst(player).x == 12) { turner = 0; }
    if (turner == 1) { getFirst(player).x += 1 * movcon; } else { getFirst(player).x -= 1 * movcon; }
    if (movcon == 0) {
      if (checkPlayerCollision(obstacle) == 1) {
        console.log("nah you missed")
        clearInterval(vainterval)
        playback.end()
        playTune(loseTune)
        handleLost()
      } else if (getFirst(player).y == 1) {
        console.log("nah you missed")
        clearInterval(vainterval)
        playback.end()
        playTune(loseTune)
        handleLost()
      }
      if (checkPlayerCollision(goal_u) == 1 || checkPlayerCollision(goal_l) == 1 || checkPlayerCollision(goal_r) == 1) {
        console.log("GOOOOOOAAAALL");
        clearInterval(vainterval)
        if(currentmap==10){
          finishTxt = addText("Game Finished\n\nPress any key\nto go to the\nfirst level", options = { x:4, y:5, color:introColor })
          finished =1
          playback.end()
          playTune(winningTune)
        }else{
          changeLevel(currentmap + 1)
          playTune(goalTune)
        }
        
      }
    }
}


introText = addText("Welcome to \n Score It\n\nThere are\n10 Levels\n\nPress any key\nto continue", options = { x:5, y:4, color:introColor })

function mover() { getFirst(player).y -= 1*ycon; }


onInput("w", () => {
  if(FirstInput == 1){
    movcon = 0
    ycon = 1
    if(!automover){automover = setInterval(mover, 80)}}
})



afterInput(() => {
  if(FirstInput == 0){
    FirstInput = 1
    clearText(introText)
    vainterval = setInterval(mainfunc,movinterval)
    levtext = addText(("Lvl: " + currentmap), options = { x:0, y:0, color:color`9` })
  }
  if(finished == 1){
    clearText(finishTxt)
    clearInterval(automover)
    automover = undefined
    changeLevel(1)
    finished =0
  }
  if(finished == 2){
    clearText(LostTxt)
    clearInterval(automover)
    automover = undefined
    changeLevel(1)
    finished =0
    
  }
  }

)
